package com.yzt.message.msgdialog;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.yzt.zhuju.arouter.RouterMessage;
import com.yzt.zhuju.config.AppConfig;
import com.yzt.zhuju.richtext.EmojiTextView;
import com.yzt.p_base.utils.DisplayUtils;
import com.yzt.plugin_imageloader.ImageLoadUtil;
import com.yzt.plugin_widget.RoundImageView;
import com.yzt.message.R;

/**
 * desc: 浮动的消息
 **/
public class MsgFloatView extends FrameLayout {

    private float downY;
    private final int dp10 = DisplayUtils.dp2px(10);
    private static final String TAG = "MsgFloatView";
    private RoundImageView mRivHead;
    private TextView mTvName;
    private EmojiTextView mTvContent;
    private String uid;
    private String name;
    private OnClickListener listener;
    private float downX;

    public MsgFloatView(@NonNull Context context) {
        super(context);
        initView();
    }

    public MsgFloatView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    private void initView() {
        View rootView = LayoutInflater.from(getContext()).inflate(R.layout.view_msg_float, this, true);
        mRivHead = rootView.findViewById(R.id.riv_head);
        mTvName = rootView.findViewById(R.id.tv_name);
        mTvContent = rootView.findViewById(R.id.tv_content);
    }

    public void set(String url, String name, CharSequence content, String uid) {
        Log.i(TAG, "消息弹窗设置的内容url：" + url + "--name:" + name + "--content:" + content + "--uid:" + uid);
        this.uid = uid;
        this.name = name;
        mTvName.setText(name);
        if (content != null) {
            mTvContent.setText(content);
        } else {
            mTvContent.setText(getContext().getString(R.string.receive_new_msg));
        }
        if (!TextUtils.isEmpty(url)) {
            loadImage(url, mRivHead, R.mipmap.icon_default_male_avatar);
        } else {
            //这个里边可能就有了，这里要保证必须有 就调用接口,如果
            //UserInfo userInfo = RongUserInfoManager.getInstance().getUserInfo((obtain.getConversationSenderId()));
//            BizLayer.getInstance().getAccountModule().getBasicUserInfoRequest(uid, new AresSubscriber<BasicUserInfoResponse>() {
//                @Override
//                protected void onSuccess(BasicUserInfoResponse mBasicUserInfoResponse) {
//                    if (mRivHead != null) {
//                        loadImage(mBasicUserInfoResponse.getPortraitUrl(), mRivHead, R.mipmap.icon_default_male_avatar);
//                    }
//                    if (mTvName != null) {
//                        mTvName.setText(mBasicUserInfoResponse.getUserName());
//                    }
//                }
//            });
        }
    }

    private void loadImage(String url, RoundImageView rivHead, int iconDefaultMaleAvatar) {
        try {
            //防止部分低版本 用 ApplicationContext 出现问题
            ImageLoadUtil.loadIv(AppConfig.context, url, rivHead, iconDefaultMaleAvatar);
        } catch (Exception e) {
            e.printStackTrace();
            ImageLoadUtil.loadIv(rivHead.getContext(), url, rivHead, iconDefaultMaleAvatar);
        }
    }

    public EmojiTextView getTvContent() {
        return mTvContent;
    }

    @Override
    public void setOnClickListener(@Nullable OnClickListener l) {
        this.listener = l;
    }

    @Override
    public boolean onTouchEvent(@NonNull MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downY = event.getY();
                downX = event.getX();
                Log.i(TAG, "y:" + downY);
                return true;
            case MotionEvent.ACTION_MOVE:
                break;
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                float y = event.getY();
                float x = event.getX();
                Log.i(TAG, "y:" + y);
                if (Math.abs(y - downY) > dp10) {
                    Log.i(TAG, "y:" + Math.abs(y - downY) + "--" + dp10);
                    if (listener != null) {
                        listener.onClick(this);
                    }
                    return true;
                } else if (Math.abs(y - downY) < dp10 && Math.abs(x - downX) < dp10) {
                    if (!TextUtils.isEmpty(uid) && !TextUtils.isEmpty(name)) {
                        RouterMessage.ConversationActivity(uid, name,false);
//                        MsgComeInShowHeadViewHelper.getInstance().resetHeadView(uid);
                    }
                    if (listener != null) {
                        listener.onClick(this);
                    }
                    return true;
                }
                break;
            default:
                break;
        }
        Log.i(TAG, "--------------");
        return super.onTouchEvent(event);
    }
}
